package com.company.cloud.page.modules.app.controller;


import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.HttpContextUtils;
import com.company.cloud.page.modules.base.entity.PageStaticInfo;
import com.company.cloud.page.modules.base.service.PageStaticInfoService;
import com.company.cloud.page.modules.mgr.controller.req.WebPageComponentConfigAdd2EditParam;
import com.company.cloud.page.modules.mgr.service.WebPageConfigMgrService;
import com.company.cloud.page.render.HtmlRender;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping("/html")
public class WebPageAppController {


    @Autowired
    private WebPageConfigMgrService webPageConfigMgrService;

    @Autowired
    private PageStaticInfoService pageStaticInfoService;



    @RequestMapping("/page/{pageNo}")
    public void page(@PathVariable String pageNo) {
        try {
            String text= webPageConfigMgrService.getPageContent(pageNo,false);
            this.render(text);
        }catch (Exception e){
            e.printStackTrace();
            String text="<dev bgcolor='white'><center><h1>500 Internal Server Error</h1></center></dev>";
            this.render(text);
        }
    }

    @RequestMapping("/page/dev/{pageNo}")
    public void devPage(@PathVariable String pageNo) {
        try {
            String text= webPageConfigMgrService.getPageContent(pageNo,true);
            this.render(text);
        }catch (Exception e){
            e.printStackTrace();
            String text="<dev bgcolor='white'><center><h1>500 Internal Server Error</h1></center></dev>";
            this.render(text);
        }
    }



    //静态页面
    @RequestMapping("/staticPage/{pageNo}")
    public void staticPage(@PathVariable String pageNo) {
        try {
            PageStaticInfo entity= pageStaticInfoService.getById(pageNo);
            if(entity==null || BaseConstant.EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())){
                throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
            }
            this.render(entity.getContent());
        }catch (Exception e){
            e.printStackTrace();
            String text="<dev bgcolor='white'><center><h1>500 Internal Server Error</h1></center></dev>";
            this.render(text);
        }
    }



    private void render(String text){
        HtmlRender render=new HtmlRender(text);
        render.setContext(HttpContextUtils.getRequest(),HttpContextUtils.getResponse());
        render.render();

    }



}
